package com.bckj.fastboot.iot.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bckj.fastboot.core.extra.cache.DoubleCacheOps;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.core.page.PageSupport;
import com.bckj.fastboot.iot.biz.convert.IotProtocolDisplaySetConvert;
import com.bckj.fastboot.iot.biz.repository.IotProtocolDisplaySetRepository;
import com.bckj.fastboot.iot.biz.service.IotProtocolDisplaySetService;
import com.bckj.fastboot.iot.common.constant.IotCacheConstants;
import com.bckj.fastboot.iot.common.dto.query.IotProtocolDisplaySetQuery;
import com.bckj.fastboot.iot.common.dto.request.IotProtocolDisplaySetRequest;
import com.bckj.fastboot.iot.common.dto.response.IotProtocolDisplaySetResponse;
import com.bckj.fastboot.iot.common.entity.IotProtocolDisplaySet;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Comparator;
import java.util.List;

/**
 * IotProtocolDisplaySetServiceImpl
 *
 * @author wfc
 **/
@Service
@RequiredArgsConstructor
public class IotProtocolDisplaySetServiceImpl implements IotProtocolDisplaySetService {

    private final IotProtocolDisplaySetConvert iotProtocolDisplaySetConvert;
    private final IotProtocolDisplaySetRepository iotProtocolDisplaySetRepository;
    private final DoubleCacheOps doubleCacheOps;

    @Override
    public boolean save(IotProtocolDisplaySetRequest request) {
        var entity = iotProtocolDisplaySetConvert.toEntity(request);
        return iotProtocolDisplaySetRepository.save(entity);
    }

    @Override
    public boolean update(IotProtocolDisplaySetRequest request) {
        var entity = iotProtocolDisplaySetConvert.toEntity(request);
        return iotProtocolDisplaySetRepository.updateById(entity);
    }

    @Override
    public IotProtocolDisplaySetResponse details(Long id) {
        return iotProtocolDisplaySetRepository.getBaseMapper().findById(id);
    }

    @Override
    public List<IotProtocolDisplaySetResponse> findList(IotProtocolDisplaySetQuery query) {
        return iotProtocolDisplaySetRepository.getBaseMapper().findList(query);
    }

    @Override
    public PageResult<IotProtocolDisplaySetResponse> page(IPage<IotProtocolDisplaySetResponse> page, IotProtocolDisplaySetQuery query) {
        var result = PageSupport.page(page, query, this::findList);
        return PageResult.of(result);
    }

    @Override
    public IotProtocolDisplaySet findOne(String code, String type) {
        return iotProtocolDisplaySetRepository.lambdaQuery()
                .eq(IotProtocolDisplaySet::getCode, code)
                .eq(IotProtocolDisplaySet::getType, type)
                .last("limit 1")
                .one();
    }

    @Override
    public List<IotProtocolDisplaySet.Item> getItems(String code, String type) {
        String cacheKey = code + type;
        return doubleCacheOps.doubleCache(IotCacheConstants.IOT_PROTOCOL_DISPLAY_SET_CACHE_NAME, cacheKey, () -> {
            IotProtocolDisplaySet one = findOne(code, type);
            Assert.notNull(one, "未找到协议展示配置");
            List<IotProtocolDisplaySet.Item> list = JsonUtils.toBean(one.getContent(), new TypeReference<>() {
            });
            if (CollUtil.isNotEmpty(list)) {
                list.sort(Comparator.comparingInt(IotProtocolDisplaySet.Item::getSort));
            }
            return list;
        });
    }

    @Override
    public void clearItemsCache(String code, String type) {
        String cacheKey = code + type;
        doubleCacheOps.clearCache(IotCacheConstants.IOT_PROTOCOL_DISPLAY_SET_CACHE_NAME, cacheKey);
    }
}
